﻿using System;
using System.Collections.Generic;
using System.CommandLine.Invocation;
using System.CommandLine;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.CommandLine.Parsing;
using System.IO;
using System.CommandLine.Help;
using Newtonsoft.Json.Linq;

namespace schtasks2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Run(args);
            Console.ReadLine();
        }

        static void Run(string[] args)
        {
            try
            {
                var createOption = new Option<bool>(
                new[] { "/create" }, () => true, "创建任务");
                var usernameOption = new Option<string>(
                    new[] { "username", "/u", "/ru" }, "输入用户名");
                var passwordOption = new Option<string>(
                    new[] { "password", "/p", "/rp" }, "输入密码");
                var tasknameOption = new Option<string>(
                    new[] { "taskname", "/tn" }, description: "输入任务名字");
                tasknameOption.IsRequired = true;
                var taskdescOption = new Option<string>(
                    new[] { "taskdesc", "/td" }, "输入任务描述");
                var taskrunOption = new Option<string>(
                    new[] { "taskrun", "/tr" }, "输入任务执行");
                taskrunOption.IsRequired = true;
                var workingDirectoryOption = new Option<string>(
                    new[] { "workingdirectory", "/wd" }, "输入工作目录");
                var scheduleOption = new Option<string>(
                    new[] { "schedule", "/sc" }, "计划频率");
                var itOption = new Option<bool>(
                    new[] { "/it" }, () => false, "以交互方式运行");
                var rlOption = new Option<string>(
                    new[] { "/rl" }, "设置任务的运行级别");
                var fOption = new Option<bool>(
                    new[] { "/f" }, () => false, "强制执行");
                var rootCommand = new RootCommand("创建计划任务工具");
                rootCommand.AddOption(createOption);
                rootCommand.AddOption(usernameOption);
                rootCommand.AddOption(passwordOption);
                rootCommand.AddOption(tasknameOption);
                rootCommand.AddOption(taskdescOption);
                rootCommand.AddOption(taskrunOption);
                rootCommand.AddOption(workingDirectoryOption);
                rootCommand.AddOption(scheduleOption);
                rootCommand.AddOption(itOption);
                rootCommand.AddOption(rlOption);
                rootCommand.AddOption(fOption);

                rootCommand.SetHandler((ParseResult parseResult) =>
                {
                    // 输出原始参数
                    //Console.WriteLine("\n原始参数:");
                    //foreach (var token in parseResult.Tokens)
                    //{
                    //    Console.WriteLine($"  {token.Type}: {token.Value}");
                    //}
                    string username = parseResult.GetValueForOption(usernameOption);
                    string password = parseResult.GetValueForOption(passwordOption);
                    string taskname = parseResult.GetValueForOption(tasknameOption);
                    //if (!parseResult.ValidateOptionNotEmpty(tasknameOption, out string taskname)) return;
                    string taskdesc = parseResult.GetValueForOption(taskdescOption);
                    string taskrun = parseResult.GetValueForOption(taskrunOption);
                    //if (!parseResult.ValidateOptionNotEmpty(taskrunOption, out string taskrun)) return;
                    string workingDirectory = parseResult.GetValueForOption(workingDirectoryOption);
                    string schedule = parseResult.GetValueForOption(scheduleOption);
                    bool it = parseResult.GetValueForOption(itOption);
                    string rl = parseResult.GetValueForOption(rlOption);
                    bool f = parseResult.GetValueForOption(fOption);

                    TaskServiceHelper.AddTask(
                        taskname,
                        taskdesc,
                        taskrun,
                        "",
                        workingDirectory,
                        username,
                        password,
                        schedule,
                        it,
                        rl,
                        f,
                        null);
                    Console.WriteLine("执行结束");
                }, new ParseResultBinder());
                rootCommand.InvokeAsync(args);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"错误: {ex.Message}");
                Console.Error.WriteLine($"堆栈: {ex.StackTrace}");
            }
        }
    }
}
